package com.example.progressbartest;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Window;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.Toast;

import com.daimajia.numberprogressbar.NumberProgressBar;
import com.daimajia.numberprogressbar.OnProgressBarListener;

import java.util.Timer;
import java.util.TimerTask;

public class MainActivity extends AppCompatActivity implements OnProgressBarListener {


//    protected void onCreate(Bundle savedInstanceState) {
//        super.onCreate(savedInstanceState);
//        setContentView(R.layout.activity_main);
//
//
//    }
    private  ProgressBar pb;
    private Handler mHandler;
    private final int MSG = 100;
    private  int pro = 0;
    EditText value;

    private Timer timer;

    private NumberProgressBar bnp;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //请求设置窗口标题栏滚动条
        requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
        setContentView(R.layout.activity_main);
        pb = (ProgressBar)findViewById(R.id.firstBar);
        value = (EditText)findViewById(R.id.value);
        //设置滚动条可见
        //setProgressBarIndeterminateVisibility(true);

        //创建一个Handler
        mHandler = new Handler(){
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                //处理消息
                switch (msg.what) {
                    case MSG:
                        //设置滚动条和text的值
                        pb.setProgress(pro);
                        value.setText(Integer.toString(pro));
                        break;
                    default:
                        break;
                }
            }
        };
        start();
        bnp = (NumberProgressBar)findViewById(R.id.numberbar1);
        bnp.setOnProgressBarListener(this);
        timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        bnp.incrementProgressBy(1);
                    }
                });
            }
        }, 1000, 100);
    }

    private void start()
    {
        new Thread(new Runnable() {
            @Override
            public void run() {
                int max = pb.getMax();
                try {
                    //子线程循环间隔消息
                    while (pro <= max) {
                        Message msg = new Message();
                        msg.what = MSG;
                        mHandler.sendMessage(msg);
                        if(pro == max)
                            pro = 0;

                        Thread.sleep(1000);
                        pro += 10;
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        timer.cancel();
    }

    @Override
    public void onProgressChange(int current, int max) {
        if(current == max) {
            Toast.makeText(getApplicationContext(), getString(R.string.finish), Toast.LENGTH_SHORT).show();
            bnp.setProgress(0);
        }
    }

}
